package cn.iocoder.yudao.module.design.controller.admin.flowtask;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.flowtask.vo.*;
import cn.iocoder.yudao.module.design.convert.flowtask.FlowTaskConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.flowtask.FlowTaskDO;
import cn.iocoder.yudao.module.design.dal.dataobject.pic.PicDO;
import cn.iocoder.yudao.module.design.service.flowtask.FlowTaskService;
import cn.iocoder.yudao.module.design.service.pic.PicService;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; 

@Tag(name = "管理后台 - 待办任务")
@RestController
@RequestMapping("/design/flowTask")
@Validated
public class FlowTaskController {

    @Resource
    private FlowTaskService flowTaskService;

    @Resource
    private PicService picService;

    @PostMapping("/create")
    @Operation(summary ="创建待办任务")
    @PreAuthorize("@ss.hasPermission('design:flow-task:create')")
    public CommonResult<Long> createFlowTask(@Valid @RequestBody FlowTaskCreateReqVO createReqVO) {
        return success(flowTaskService.createFlowTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新待办任务")
    @PreAuthorize("@ss.hasPermission('design:flow-task:update')")
    public CommonResult<Boolean> updateFlowTask(@Valid @RequestBody FlowTaskUpdateReqVO updateReqVO) {
        flowTaskService.updateFlowTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除待办任务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:flow-task:delete')")
    public CommonResult<Boolean> deleteFlowTask(@RequestParam("id") Long id) {
        flowTaskService.deleteFlowTask(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得待办任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:flow-task:query')")
    public CommonResult<FlowTaskRespVO> getFlowTask(@RequestParam("id") Long id) {
        FlowTaskDO flowTask = flowTaskService.getFlowTask(id);
        return success(FlowTaskConvert.INSTANCE.convert(flowTask));
    }

    @GetMapping("/getFlowTaskByNodeId")
    @Operation(summary ="获得待办任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:flow-task:query')")
    public CommonResult<FlowTaskRespVO> getFlowTaskByNodeId(@RequestParam("id") Long id) {
        FlowTaskDO flowTask = flowTaskService.getFlowTaskByNodeId(id);
        return success(FlowTaskConvert.INSTANCE.convert(flowTask));
    }

    @GetMapping("/list")
    @Operation(summary ="获得待办任务列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:flow-task:query')")
    public CommonResult<List<FlowTaskRespVO>> getFlowTaskList(@RequestParam("ids") Collection<Long> ids) {
        List<FlowTaskDO> list = flowTaskService.getFlowTaskList(ids);
        return success(FlowTaskConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得待办任务分页")
    @PreAuthorize("@ss.hasPermission('design:flow-task:query')")
    public CommonResult<PageResult<FlowTaskPageRespVO>> getFlowTaskPage(@Valid FlowTaskPageReqVO pageVO) {
        PageResult<FlowTaskDO> pageResult = flowTaskService.getFlowTaskPage(pageVO);
        // 获得拼接需要的数据

        // 拼接结果返回
        List<FlowTaskPageRespVO> flowTaskRespVOList = new ArrayList<>(pageResult.getList().size());

        pageResult.getList().forEach(task -> {
            FlowTaskPageRespVO respVO = FlowTaskConvert.INSTANCE.convertPageRes(task);
            PicDO picDO = new PicDO();
            picDO.setHouseId(task.getHouseId());
            picDO.setItemId(task.getId());
            picDO.setUploadType("0");
            picDO.setSourceType("5");
            List<PicDO> picData = picService.getPicData(picDO);
            respVO.setFileList(picData);
            flowTaskRespVOList.add(respVO);

        });

        return success(new PageResult<>(flowTaskRespVOList, pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出待办任务 Excel")
    @PreAuthorize("@ss.hasPermission('design:flow-task:export')")
   
    public void exportFlowTaskExcel(@Valid FlowTaskExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FlowTaskDO> list = flowTaskService.getFlowTaskList(exportReqVO);
        // 导出 Excel
        List<FlowTaskExcelVO> datas = FlowTaskConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "待办任务.xls", "数据", FlowTaskExcelVO.class, datas);
    }

    @GetMapping("/all")
    @Operation(summary ="获得待办任务")
//    @PreAuthorize("@ss.hasPermission('design:flow-task:query')")
    public CommonResult<List<FlowTaskRespVO>> getFlowTaskList(@Valid FlowTaskBaseVO vo) {
        List<FlowTaskDO> flowTaskList= flowTaskService.getFlowTaskList(vo);
        return success(FlowTaskConvert.INSTANCE.convertList(flowTaskList));
    }

    @GetMapping("/submit")
    @Operation(summary ="流转待办任务")
    @PreAuthorize("@ss.hasPermission('design:flow-task:submit')")
    public CommonResult<Boolean> submit(@RequestParam("id") Long id) {
        flowTaskService.submitFlowTaskList(id);
        return success(true);
    }
    @GetMapping("/submitByNodeId")
    @Operation(summary ="通过节点Id流转待办任务")
    @PreAuthorize("@ss.hasPermission('design:flow-task:submit')")
    public CommonResult<Boolean> submitByNodeId(@RequestParam("id") Long id) {
        FlowTaskCustomizeVo flowTaskCustomizeVo = new FlowTaskCustomizeVo();
        flowTaskCustomizeVo.setNodeId(id);
        flowTaskCustomizeVo.setMethodName("addPu");
        JSON json =(JSON)JSON.parse("{\"xx\":\"11\"}");
        flowTaskCustomizeVo.setParams(json);
        flowTaskService.submitFlowTaskListByNodeId(flowTaskCustomizeVo);
        return success(true);
    }

    @PostMapping("/replenish")
    @Operation(summary ="补充待办任务")
    @PreAuthorize("@ss.hasPermission('design:flow-task:submit')")
    public CommonResult<Boolean> replenish(@Valid @RequestBody FlowTaskBaseVO vo) {
        flowTaskService.replenishTask(vo);
        return success(true);
    }

    @PostMapping("/replenishByNodeId")
    @Operation(summary ="补充待办任务")
    @PreAuthorize("@ss.hasPermission('design:flow-task:submit')")
    public CommonResult<Boolean> replenishByNodeId(@Valid @RequestBody FlowTaskBaseVO vo) {
        flowTaskService.replenishTask(vo);
        return success( true);
    }

    @PostMapping("/rollbackTask")
    @Operation(summary ="回退任务")
    @PreAuthorize("@ss.hasPermission('design:flow-task:submit')")
    public CommonResult<Boolean> rollbackTask(@Valid @RequestBody FlowTaskBaseVO vo) {
        flowTaskService.rollbackTask(vo);
        return success(true);
    }



    @PostMapping("/dealFolwTask")
    @Operation(summary ="处理任务")
    @PreAuthorize("@ss.hasPermission('design:flow-task:query')")
    public CommonResult<Object> dealFolwTask(@Valid @RequestBody CommonFlowTaskActionVo vo) throws Throwable {
        Object task = flowTaskService.dealFolwTask(vo);
        return success(task);
    }
}
